﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Messenger.WinDialogs.Controls
{
    /// <summary>
    /// DialogWin.xaml 的交互逻辑
    /// </summary>
    public partial class DialogWin : Window
    {
        public ShowWinDialogContext ShowContext { get; set; }

        public MessageBoxResult MessageBoxResult { get; set; }

        public static event Action Completed;

        public DialogWin(ShowWinDialogContext context)
        {
            InitializeComponent();

            if (context.SizeToContent != SizeToContent.Manual)
            {
                this.SizeToContent = context.SizeToContent;
            }

            if (context.WinStyle != null)
            {
                this.Style = context.WinStyle;
            }

            if (context.PositiveBtnStyle != null)
            {
                this.OKBtn.Style = context.PositiveBtnStyle;
                this.YesBtn.Style = context.PositiveBtnStyle;
            }

            if (context.NegativeBtnStyle != null)
            {
                this.NoBtn.Style = context.NegativeBtnStyle;
                this.CancelBtn.Style = context.NegativeBtnStyle;
            }

            this.DataContext = this;
            this.ShowContext = context;
            this.Title = context.Title;
            if (context.Owner != null)
            {
                this.Owner = context.Owner;
                this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }
            ContentContainer.Content = context.View;
            MessageBoxButtonPanel.Visibility = context.ShowMessageBoxButton ? Visibility.Visible : Visibility.Collapsed;
            Completed?.Invoke();
        }

        #region EventHandlers

        #region MessageBoxButton Handlers

        private void OKBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.OK;
            this.DialogResult = true;
        }

        private void YesBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Yes;
            this.DialogResult = true;
        }

        private void NoBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.No;
            this.DialogResult = false;
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Cancel;
            this.Close();
        }

        #endregion MessageBoxButton Handlers	

        #endregion EventHandlers	
    }

    internal class MessageBoxButtonVisibilityConverter : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is MessageBoxButton messageBoxButton && parameter is string str)
            {
                if (messageBoxButton.ToString().Contains(str))
                {
                    return Visibility.Visible;
                }
            }

            return Visibility.Collapsed;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
